<?php

declare (strict_types = 1);

namespace app\common\service\permission;
use app\common\service\BaseService;
use app\common\repository\permission\RoleRepository;

class RoleService extends BaseService
{
    public function __construct(RoleRepository $repository)
    {
        $this->repository = $repository;
        parent::__construct($repository);
    }

    /**
     * 获取角色分页列表
     *
     * @param array $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function getList(array $where = []): array
    {
        [$page, $pageSize] = $this->getPaginationParams();
        unset($where['page'], $where['page_size']);
        return $this->repository->paginate($where, ['order' => 'desc'], $page, $pageSize);
    }

    /**
     * 获取角色详情信息
     *
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($id): array
    {
        $data = $this->repository->getOne($id)->toArray();
        $data['menu_ids'] = explode(',', $data['menu_ids']);
        return $data;
    }

    /**
     * 创建角色信息
     *
     * @param array $params
     * @return \app\common\model\BaseModel|array|\think\Collection
     * @throws \Exception
     */
    public function create(array $params): \think\Collection|\app\common\model\BaseModel|array
    {
        $data = [
            'name' => $params['name'],
            'code' => $params['code'],
            'order'=> $params['order'],
            'status' => $params['status'],
            'desc' => $params['desc'],
        ];
        return $this->repository->save($data);
    }

    /**
     * 更新角色信息
     *
     * @param array $params
     * @param $id
     * @return \app\common\model\BaseModel|bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(array $params, $id): \app\common\model\BaseModel|bool|int
    {
        if(empty($id)) {
            throw new \Exception('ID 不能为空');
        }
        $data = [
            'name' => $params['name'],
            'code' => $params['code'],
            'order'=> $params['order'],
            'status' => $params['status'],
            'desc' => $params['desc']
        ];
        if(!empty($params['menu_ids'])) {
            $data['menu_ids'] = implode(',', $params['menu_ids']);
        }
        return $this->repository->update($data, $id);
    }

    /**
     * 设置角色权限
     *
     * @param $params
     * @param $id
     * @return \app\common\model\BaseModel|bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setRoleAuth($params, $id): \app\common\model\BaseModel|bool|int
    {
        if(empty($id)) {
            throw new \Exception('ID 不能为空');
        }
        $data['menu_ids'] = implode(',', $params);
        return $this->repository->update($data, $id);
    }

    /**
     * 删除角色信息
     *
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($id): bool
    {
        return $this->repository->delete($id);
    }
}